home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Warrior’s Progress / source code / Source / Libraries / Menus / High Level / Menu.cp < prev    next >
Encoding:
Text File  |  1997-06-28  |  2.3 KB  |  113 lines  |  [TEXT/CWIE]

  1. // Menu.cp
  2.  
  3. #ifndef Menu_h
  4. #include "Menu.h"
  5. #endif
  6. #ifndef MenuBar_h
  7. #include "MenuBar.h"
  8. #endif
  9. #ifndef MenuSection_h
  10. #include "MenuSection.h"
  11. #endif
  12. #ifndef MinMax_h
  13. #include "MinMax.h"
  14. #endif
  15.  
  16. Menu::Menu( MenuID id, ConstPString title )
  17.   : MenuObject( id, title ),
  18.      sectionCount( 0 ),
  19.      itemCount( 0 ),
  20.      link( this )
  21.   {
  22.     for ( uint16 i = 0; i < maxSections; i++ )
  23.       {
  24.         sections[i] = 0;
  25.         items[i] = 0;
  26.       }
  27.     MenuBar::The().Add( link, afterEnd );
  28.   }
  29.  
  30. Menu::Menu( ResourceID resource )
  31.   : MenuObject( resource ),
  32.      sectionCount( 0 ),
  33.      itemCount( 0 ),
  34.      link( this )
  35.   {
  36.     for ( uint16 i = 0; i < maxSections; i++ )
  37.       {
  38.         sections[i] = 0;
  39.         items[i] = 0;
  40.       }
  41.     MenuBar::The().Add( link, afterEnd );
  42.   }
  43.  
  44. void Menu::AddFixedSection( MenuSection& section, uint16 length )
  45.   {
  46.     Assert( sectionCount < maxSections );
  47.     Assert( length > 0 );
  48.     Assert( length < maxSections );
  49.     Assert( itemCount + length < maxSections );
  50.     
  51.     Assert( sections[ sectionCount ] == 0 );
  52.     sections[ sectionCount++ ] = §ion;
  53.     
  54.     uint16 firstItem = itemCount;
  55.     itemCount += length;
  56.     for ( uint32 i = firstItem; i < itemCount; i++ )
  57.       {
  58.         Assert( items[ i ] == 0 );
  59.         items[ i ] = §ion;
  60.       }
  61.   }
  62.  
  63. void Menu::AddExtensibleSection( MenuSection& section )
  64.   {
  65.     Assert( sectionCount < maxSections );
  66.     Assert( itemCount < maxSections );
  67.     
  68.     Assert( sections[ sectionCount ] == 0 );
  69.     sections[ sectionCount++ ] = §ion;
  70.     
  71.     uint16 firstItem = itemCount;
  72.     itemCount = maxSections;
  73.     for ( uint32 i = firstItem; i < itemCount; i++ )
  74.       {
  75.         Assert( items[ i ] == 0 );
  76.         items[ i ] = §ion;
  77.       }
  78.   }
  79.  
  80. void Menu::Prepare()
  81.   {
  82.     // Make sure we've added all our items
  83.     Assert( itemCount == maxSections || itemCount == Length() );
  84.     for ( uint32 i = 0; i < sectionCount; i++ )
  85.       {
  86.         Assert( sections[i] != 0 );
  87.         sections[i]->Prepare();
  88.       }
  89.     
  90.     EnableIfItemsEnabled();
  91.   }
  92.  
  93. void Menu::Choose( uint16 item )
  94.   {
  95.     Assert( item < Length() );
  96.     
  97.     uint32 sectionNumber = item;
  98.     if ( sectionNumber >= maxSections )
  99.         sectionNumber = maxSections;
  100.     
  101.     Assert( sectionNumber < itemCount );
  102.     Assert( items[ sectionNumber ] != 0 );
  103.     MenuSection& section( *items[ sectionNumber ] );
  104.     
  105.     Assert( item >= section.FirstItem() );
  106.     Assert( item < section.FirstItem() + section.MaxLength() );
  107.     
  108.     section.Choose( item - section.FirstItem() );
  109.   }
  110.  
  111. #include "ListLink.cp"
  112. #include "ListOf.cp"
  113.